package com.ztgf.commodity.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.BrandInfoBO;
import com.ztgf.commodity.models.bo.BrandInfoSaveBO;
import com.ztgf.commodity.models.dto.BrandInfoDTO;
import com.ztgf.commodity.models.service.IBrandInfoService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-25 14:15
 * Description:
 */
@RestController
@RequestMapping("/api/brandInfo")
@Slf4j
@Api(value = "BrandInfoApiController", tags = "商品品牌表接口")
public class BrandInfoApiController {

    @DubboReference
    private IBrandInfoService brandInfoApiService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加商品品牌表", notes = "添加商品品牌表")
    public Result insertBrandInfo(BrandInfoSaveBO saveBO) {
        if (brandInfoApiService.updateByName(saveBO.getName())){
            return new Result<>(CodeEnum.FAIL_100043);
        }

        boolean flag = brandInfoApiService.addBrand(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/queryPageList")
    @ApiOperation(value = "商品品牌列表", notes = "商品品牌列表")
    public Result<Page<BrandInfoDTO>> queryPageList(BrandInfoBO brandInfoBO) {
        Wrapper<BrandInfoDTO> wrapper = new EntityWrapper<>();
        if (brandInfoBO.getName() != null) {
            wrapper.like("name", brandInfoBO.getName());
        }
        if (brandInfoBO.getInitials() != null) {
            wrapper.like("initials", brandInfoBO.getInitials());
        }
        wrapper.eq("is_publish", 1);
        wrapper.eq("status", 0);
        Page<BrandInfoDTO> page = new Page<>(brandInfoBO.getPageNo(), brandInfoBO.getPageSize());
        page = brandInfoApiService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @PostMapping("/queryList")
    @ApiOperation(value = "所有商品品牌列表", notes = "所有商品品牌列表")
    @ApiImplicitParam(name = "initials",value = "initials", paramType = "query", dataType = "String")
    public Result<List<BrandInfoDTO>> queryList(String initials, Long shopId) {
//        Wrapper<BrandInfoDTO> wrapper = new EntityWrapper<>();
//        wrapper.eq("is_publish", 1);
//        wrapper.eq("status", 0);
//        if (StringUtil.isNotEmpty(initials)){
//            wrapper.like("initials", initials);
//        }

        List<BrandInfoDTO> brandInfoDTOList = brandInfoApiService.queryList(initials, shopId);
        return new Result<>(brandInfoDTOList);
    }

    @PostMapping("/queryBrandList")
    @ApiOperation(value = "品牌列表-用于商品上传页", notes = "品牌列表-用于商品上传页")
    public Result queryBrandList() {
        Wrapper<BrandInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_publish", 1);
        wrapper.eq("status", 0);
        return new Result<>(brandInfoApiService.selectList(wrapper));
    }

    @PostMapping("/queryShopBrandList")
    @ApiOperation(value = "查询店铺下所有品牌", notes = "查询店铺下所有品牌")
    public Result queryShopBrandList(Long shopId) {
        if (shopId == null) {
            return new Result(CodeEnum.FAIL_100002);
        }
        return new Result<>(brandInfoApiService.queryShopBrandList(shopId));
    }

    @PostMapping("/getByName")
    @ApiOperation(value = "根据品牌名称判断是否存在", notes = "根据品牌名称判断是否存在")
    public Result getByName(String name) {
        if (name == null || "".equals(name)) {
            return new Result(CodeEnum.FAIL_100002);
        }

        if (brandInfoApiService.updateByName(name)){
            return new Result<>(CodeEnum.FAIL_100043);
        }else {
            return new Result<>();
        }
    }
}
